package com.jvyou.thread.d11deadlock;

public class DeadlockExample {
    public static void main(String[] args) {
        final Object chopsticks1 = new Object();
        final Object chopsticks2 = new Object();

        // 线程1
        Thread boy = new Thread(() -> {
            synchronized (chopsticks1) {
                System.out.println("男孩: 拥有第1根筷子...");

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("男孩: 等待第2根筷子...");
                synchronized (chopsticks2) {
                    System.out.println("男孩: 拥有了第1根和第2根筷子，可以吃菜...");
                }
            }
        });

        // 线程2
        Thread girl = new Thread(() -> {
            synchronized (chopsticks2) {
                System.out.println("女孩：拥有第2根筷子...");

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("女孩: 等待第1根筷子...");
                synchronized (chopsticks1) {
                    System.out.println("女孩: 拥有了第1根和第2根筷子，可以吃菜...");
                }
            }
        });

        boy.start();
        girl.start();
    }
}
